×
☰ See All Chapters

Spring @Import and @ImportResource Annotation

When spring configuration increases, having single configuration class would be not sufficient. We should split the configurations into multiple configuration classes. By doing this we can organize the configurations to different divisions. When we have multiple configuration classes, supplying all those to spring context will be clumsy when compared to providing a single configuration class to spring context. Spring @Import annotation is used to import one or more @Configuration classes into one configuration class and then we supply this one configuration class to spring context. It works same as <import/> in XML configuration.

Even if spring has annotation configuration there may be a chances, application can still have xml configuration files. The @ImportResource annotation is used to import one or more XML configuration files.

Spring @Import Annotation Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring3_Import</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

Apple.java

package com.java4coding;

 

public class Apple{

        public Apple() {

                System.out.println("Apple bean instantiated");

        }

}

AppleConfiguration.java

package com.java4coding;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

 

@Configuration

public class AppleConfiguration {

       

    @Bean

    public Apple getApple() {

        return new Apple();

    }

 

}

Mango.java

package com.java4coding;

 

public class Mango {

        public Mango() {

                System.out.println("Mango bean instantiated");

        }

}

MangoConfiguration.java

package com.java4coding;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

 

@Configuration

public class MangoConfiguration {

       

    @Bean

    public Mango getMango() {

        return new Mango();

    }

 

}

SpringConfiguration.java

package com.java4coding;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Import;

 

@Configuration

@Import({AppleConfiguration.class, MangoConfiguration.class})

public class SpringConfiguration {

       

}

Demo.java

package com.java4coding;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

public class Demo {

        public static void main(String[] args) {

                ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);

                ((ConfigurableApplicationContext)context).close();

               

        }

}

Output:

spring-import-and-importresource-annotation-0
 

All Chapters
Author